Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
JavaFX: How to bind text field "style property" to text property matching some pattern

In my application I have a screen with three fields which I need to validate, and a Save button.

I bind save button "disable property" to fields' "text property" matching some pattern (simplified patterns in this example, doesn't matter):

public void initialize(){
    saveNetworkBtn.disableProperty()
            .bind(textPropertyBindingPattern(ipInp, Pattern.compile("[0-9]{3}"))
                    .or(textPropertyBindingPattern(subnetInp, Pattern.compile("[0-9]{3}")))
                    .or(textPropertyBindingPattern(gatewayInp, Pattern.compile("[0-9]{3}"))));
}

private BooleanBinding textPropertyBindingPattern(TextField textField, Pattern pattern ) {
    return Bindings.createBooleanBinding(() ->
            !pattern.matcher(textField.getText()).matches(), textField.textProperty());
}

So that save button is locked until all values are correct:enter image description here

But I need to highlight incorrect fields also with some red border like

-fx-border-color: red;

enter image description here

My main goal is to handle this in one place instead of adding several listeners. Is it possible to bing this somehow in the code above or I need to add listeners to each field?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Solved by adding a listener to binding, which adds a style to received text field:

private BooleanBinding textPropertyBindingPattern(TextField textField, Pattern pattern) {
    BooleanBinding binding = Bindings.createBooleanBinding(() ->
            !pattern.matcher(textField.getText()).matches(), textField.textProperty());

    applyValidationClass(textField, binding.get());

    binding.addListener((obs, oldValue, newValue) -> {
        applyValidationClass(textField, newValue);
    });
    return binding ;
}

private void applyValidationClass(TextField textField, boolean isInvalid) {
    textField.pseudoClassStateChanged(PseudoClass.getPseudoClass("incorrect-value"), isInvalid);
}

and 'incorrect-value' described in CSS file:

TextField:incorrect-value {
    -fx-border-color: red;
}

Visualisation: https://www.screencast.com/t/4NKEjjopvaE7

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda